﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TS.Entities;
using TS.Commons;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Globalization;

namespace TS.DataAccess
{
    /// <summary>
    /// Author: VuongPT
    /// </summary>
    public class MonthWeekDataAccess
    {

        /// <summary>
        /// Parse to Entity
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        public static MonthWeek ParseToEntity(IDataRecord dataRecord)
        {
            return new MonthWeek
            {
                DateId = dataRecord["DateId"].ToStringOrDefault().ToNumeric(),
                Year = dataRecord["Year"].ToStringOrDefault().ToNumeric(),
                Month = dataRecord["Month"].ToStringOrDefault().ToNumeric(),
                Week = dataRecord["Week"].ToStringOrDefault().ToNumeric()
            };
        }

        /// <summary>
        /// Get All MonthWeek
        /// </summary>
        /// <returns></returns>
        public Collection<MonthWeek> GetAllMonthWeek()
        {
            Collection<SqlParameter> collection = new Collection<SqlParameter>
            {
            };
            int totalRecord = 0;
            using (IDataReader reader = SqlHelper.ExecuteReader(UIMonthWeek.SelectAllMonthWeekQuery, collection))
            {
                var result = new Collection<MonthWeek>();
                while (reader.Read())
                {
                    MonthWeek obj = ParseToEntity(reader);
                    result.Add(obj);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }

        }

        /// <summary>
        /// Insert Month Week
        /// </summary>
        /// <param name="monthWeek"></param>
        /// <returns></returns>
        public int InsertMonthWeek(MonthWeek monthWeek)
        {
            int result = 0;
            if (monthWeek != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@Year",monthWeek.Year),
                                                              new SqlParameter("@Month",monthWeek.Month),
                                                              new SqlParameter("@Week",monthWeek.Week)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UICommand.InsertCommandQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["DateId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }
    }
}
